﻿$.namespace = function () {
    var a = arguments, o = null, i, j, d;
    for (i = 0; i < a.length; i = i + 1) {
        d = a[i].split(".");
        o = window;
        for (j = 0; j < d.length; j = j + 1) {
            o[d[j]] = o[d[j]] || {};
            o = o[d[j]];
        }
    }
    return o;
};

// define WuDada namespace
$.namespace('jQuery.WuDada');

(function ($) {

    $.WuDada.Core = {
        RootUrl: "",        

        Post: function (url, param, callback, exceptioncallback) {
            this.post(url, param, callback, exceptioncallback, false);
        },

        Post: function (url, param, callback, exceptioncallback, async) {
            // shift arguments if data argument was omited
            if ($.isFunction(param)) {
                callback = param;
                param = null;
            }

            $.ajax({
                type: "POST",
                url: url,
                data: param,
                async: (async == true ? true : false),
                success: function (data) {
                    try
                    {
                        var json = JSON.parse(data);
                        if (json.Success) {
                            if ($.isFunction(callback)) {
                                callback(json.Data);
                            };
                        }
                        else {
                            if( $.isFunction(exceptioncallback)) {
                                exceptioncallback(json.Data);
                            }else{
                                $.showAlert(json.Message);
                            }
                        }
                    }
                    catch(err)
                    {
                        if( $.isFunction(exceptioncallback)) {
                            exceptioncallback(data);
                        }else{
                            $.showAlert(err.toString());
                        }
                    }
                }
            });
        },

        GetJSON: function (url, param, callback, exceptioncallback) {
            this.getJSON(url, param, callback, exceptioncallback, false);
        },

        GetJSON: function (url, param, callback, exceptioncallback, async) {
            //shift arguments if data argument was omited
            if ($.isFunction(param)) {
                callback = param;
                param = null;
            }

            $.ajax({
                type: "GET",
                url: url,
                data: param,
                async: false,
                success: function (data) {
                    try
                    {
                        var json = JSON.parse(data);
                        if (json.Success) {
                            if ($.isFunction(callback)) {
                                callback(json.Data);
                            };
                        }
                        else {
                            if ($.isFunction(exceptioncallback)) {
                                exceptioncallback(json.Data);
                            }else{
                                $.showAlert(json.Message);
                            }
                        }
                    }
                    catch(err)
                    {
                        if( $.isFunction(exceptioncallback)) {
                            exceptioncallback(data);
                        }else{
                            $.showAlert(err.toString());
                        }
                    }
                }
            });
        },

        IsSafari: false
    };

})(jQuery);

(function () { 
    var WuDada = {};

    window.WuDada = WuDada;
}) (window);

function HtmlEncode(value) {
    if (value == null || value == undefined)
    { return ""; }
    return $('<div/>').text(value).html();
}

function HtmlDecode(value) {
    if (value == null || value == undefined)
    { return ""; }
    return $('<div/>').html(value).text();
}

function OverMaxLength(text, maxLength) {
    if (text.length > maxLength) {
        return true;
    }
    else {
        return false;
    }
}

function IsRedirectToLogin(data) {
    if (data.toString().indexOf("<form name=\"form1\" method=\"post\" action=\"login.aspx") > -1) {
        return true;
    }
    else {
        return false;
    }
}

function ValidatorDate(obj) {
    if (String(obj.value).length > 0) {
        var keptTimeString = "";
        if (obj.value.split(' ').length > 1) {
            keptTimeString = obj.value.split(' ')[1];
        }

        var validateDateArray = obj.value.split('/');
        if (validateDateArray.length < 3) {
            alert(validatorDateAlertString);
            obj.select(); obj.focus(); return false;
        }

        if (keptTimeString != "") {
            obj.value = validateDateArray[0] + "/" + AddZeroForValidate(parseInt(validateDateArray[1], 10)) + "/" + AddZeroForValidate(parseInt(validateDateArray[2], 10)) + " " + keptTimeString;
        } else {
            obj.value = validateDateArray[0] + "/" + AddZeroForValidate(parseInt(validateDateArray[1], 10)) + "/" + AddZeroForValidate(parseInt(validateDateArray[2], 10));
        }
    }
    var re;
    re = /([0-9])(\d\d\d)(\/)((0)([1-9])|(1)([0-2]))(\/)(([0-2])(\d)|(3)(0|1))/;

    if ((!re.test(obj.value)) && (!(String(obj.value).length == 0))) {
        alert(validatorDateAlertString);
        obj.select(); obj.focus(); return false;
    }
    else {
        if (!(String(obj.value).length == 0)) {
            //判斷 月 30 天及 2月29日
            re = /(((0)(2|4|6|9)|(1)(1))(\/)(3)(1)|(0)(2)(\/)(3)(0)|(\d)(\d)(\/)(0)(0))/;
            if (re.test(ValidatorDateMid(obj.value, 6, 5)) || ValidatorDateMid(obj.value, 6, 5) == '02/29' && !(parseInt(ValidatorDateLeft(obj.value, 4), 10) % 4) == 0) {
                alert(validatorDateAlertString);
                obj.select(); obj.focus(); return false;
            }
        }
    }
}

function ValidatorDateMid(str, start, len) {
    if (start < 0 || len < 0) return '';
    var iEnd, iLen = String(str).length;
    if (start + len > iLen)
        iEnd = iLen;
    else
        iEnd = start - 1 + len;
    return String(str).substring(start - 1, iEnd);
}

function ValidatorDateLeft(str, n) {
    if (n <= 0)
        return '';
    else if (n > String(str).length)
        return str;
    else
        return String(str).substring(0, n);
}

function AddZeroForValidate(number) {
    if (number < 10) {
        return "0" + number;
    }
    else {
        return number;
    }
}

function ReSizeBlockUI(parentObj) {
    if ($(".blockPage").outerHeight() > $(window).height()) {
        $("#" + parentObj).css("height", $(window).height() / 2);
    }
}

String.format = function (text) {
    //check if there are two arguments in the arguments list
    if (arguments.length <= 1) {
        //if there are not 2 or more arguments there’s nothing to replace
        //just return the original text
        return text;
    }
    //decrement to move to the second argument in the arra
    var tokenCount = arguments.length - 2;
    for (var token = 0; token <= tokenCount; token++) {
        //iterate through the tokens and replace their placeholders from the original text in order
        text = text.replace(new RegExp("\\{" + token + "\\}", "gi"),
                                                    arguments[token + 1]);
    }
    return text;
};


